//
//  DLPullMenuFilterView.m
//  DGGUISpaceSDK
//
//  Created by 伍明鹏 on 2019/12/20.
//

#import "DLPullMenuFilterView.h"
#import "DLPullMenuFilterNavView.h"
#import "DLFooterConfimCancelView.h"
#import "DLHeaderSelectTabView.h"


#define LINE_COLOR kRGB(229, 229, 229)
#define CONTENTVIEW_HEIGHT SCREEN_HEIGHT - 170
#define kBottomHeight 55

@interface DLPullMenuFilterView ()

//backView
@property (nonatomic, strong) UIView *backView;
//downView
@property (nonatomic, strong) UIView *contentView;
//记录分类当前选中的row
@property (nonatomic, assign) NSIndexPath *currentPIndexPath;
//顶部返回View
@property (nonatomic, strong) DLPullMenuFilterNavView *navView;
//顶部Tab标签View
@property (nonatomic, strong) DLHeaderSelectTabView *headerSelectTabView;
//底部取消确定View
@property (nonatomic, strong) DLFooterConfimCancelView *footerConfimCancelView;
//customContentView
@property (nonatomic, strong) UIView *showContentView;

@property (nonatomic, assign) CGFloat oriHeight;

@end

@implementation DLPullMenuFilterView

- (instancetype)init
{
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (instancetype)initWithCustomHeight:(CGFloat)customHeight{
    if (self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]) {
        self.oriHeight = customHeight;
        [self setupUI];
    }
    return self;
}

#pragma mark - 点击取消
- (void)cancelClick
{
    [self hideAnimation];
}

#pragma mark - 显示动画
- (void)showAnimation
{
    self.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.contentView.transform = CGAffineTransformIdentity;
        self.footerConfimCancelView.transform = CGAffineTransformIdentity;
        self.backView.alpha = 1;
    } completion:^(BOOL finished) {
        if (self.selectTabClick) {
            self.selectTabClick(self.selectTabArray[0]);
        }
    }];
}

#pragma mark - 隐藏动画
- (void)hideAnimation
{
    [UIView animateWithDuration:0.3 animations:^{
        self.backView.alpha = 0;
        self.contentView.transform = CGAffineTransformMakeTranslation(0, -self.oriHeight);
        self.footerConfimCancelView.transform = CGAffineTransformMakeTranslation(0, -self.oriHeight);
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *tTouch = [touches anyObject];
    if ([tTouch.view isEqual:self.backView]) {
        [self hideAnimation];
    }
}

#pragma mark - initView
- (void)setupUI
{
    self.hidden = YES;
    if (self.subviews.count) {
        for (UIView *tView in self.subviews) {
            [tView removeFromSuperview];
        }
    }
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    
    [self addSubview:self.backView];
    [self.backView addSubview:self.contentView];
    //标题View
    self.navView = [DLPullMenuFilterNavView new];
    self.navView.title = @"选择分类";
    @weakify(self);
    self.navView.backClick = ^{
        @strongify(self);
        [self cancelClick];
    };
    [self.contentView addSubview:self.navView];
    
    //标签View
    self.headerSelectTabView = [DLHeaderSelectTabView new];
    self.headerSelectTabView.lineColor = self.lineColor;
    self.headerSelectTabView.selectClick = ^(DLHeaderSelectTabModel *selectModel) {
        @strongify(self);
        if (self.selectTabClick) {
            self.selectTabClick(selectModel);
        }
    };
    [self.contentView addSubview:self.headerSelectTabView];
    
    if (_customContentView) {
        self.showContentView = _customContentView();
        
    }
    UIView *showContentView = [[UIView alloc] init];
    showContentView.backgroundColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor whiteColor] darkColor:[UIColor colorWithHex:0x2D2D2E]];
    self.showContentView = showContentView;
    [self.contentView addSubview:self.showContentView];
    
    //底部标签栏
    self.footerConfimCancelView = [DLFooterConfimCancelView new];
    self.footerConfimCancelView.lineColor = self.lineColor;
    __weak typeof(self)wSelf = self;
    self.footerConfimCancelView.cancelClick = ^{
        [wSelf cancelClick];
    };
    self.footerConfimCancelView.confirmClick = ^{
        if (wSelf.selectConfirmClick) {
            wSelf.selectConfirmClick();
        }
        [wSelf cancelClick];
    };
    [self.backView addSubview:self.footerConfimCancelView];
    
    
    //布局
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.backView);
        make.height.mas_equalTo(self.oriHeight);
    }];
    
    [self.navView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.contentView);
        make.height.mas_equalTo(kNavHeight);
    }];
    
    [self.headerSelectTabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.navView);
        make.top.equalTo(self.navView.mas_bottom);
        make.height.mas_equalTo(kBarHeight);
    }];
    
    [self.showContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.headerSelectTabView);
        make.top.equalTo(self.headerSelectTabView.mas_bottom);
        make.height.mas_equalTo(self.oriHeight-kNavHeight-kBarHeight);
    }];
    
    [self.footerConfimCancelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.backView);
        make.top.equalTo(self.contentView.mas_bottom);
        make.height.mas_equalTo(kBottomHeight);
    }];
    
    self.contentView.transform = CGAffineTransformMakeTranslation(0, -self.oriHeight);
    self.footerConfimCancelView.transform = CGAffineTransformMakeTranslation(0, -self.oriHeight);
}


#pragma mark - lazy

- (void)setSelectTabArray:(NSArray<DLHeaderSelectTabModel *> *)selectTabArray{
    _selectTabArray = selectTabArray;
    self.headerSelectTabView.selectTabArray = _selectTabArray;
}

- (void)setFooterViewHidden:(BOOL)footerViewHidden{
    _footerViewHidden = footerViewHidden;
    if (_footerViewHidden) {
        [UIView animateWithDuration:1.5 animations:^{
            self.footerConfimCancelView.hidden = YES;
        }];
        [self.contentView round:15 RectCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight];
        [self.footerConfimCancelView round:0 RectCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight];
    }else{
        [UIView animateWithDuration:1.5 animations:^{
            self.footerConfimCancelView.hidden = NO;
        }];
        [self.contentView round:0 RectCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight];
        [self.footerConfimCancelView round:15 RectCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight];
    }
}
- (void)setCustomContentView:(UIView * _Nonnull (^)(void))customContentView{
    _customContentView = customContentView;
    UIView *view = _customContentView();
    [self.showContentView addSubview:view];
    view.frame = self.showContentView.bounds;
}

- (void)setLineColor:(UIColor *)lineColor{
    _lineColor = lineColor;
    self.headerSelectTabView.lineColor = _lineColor;
    self.footerConfimCancelView.lineColor = _lineColor;
}

- (void)setCustomTintColor:(UIColor *)customTintColor{
    _customTintColor = customTintColor;
    self.headerSelectTabView.selectColor = _customTintColor;
    self.footerConfimCancelView.customTintColor = _customTintColor;
}
- (void)setShowTitle:(NSString *)showTitle{
    _showTitle = showTitle;
    self.navView.title = _showTitle;
}
- (void)setCancelTitle:(NSString *)cancelTitle{
    _cancelTitle = cancelTitle;
    self.footerConfimCancelView.cancelTitle = _cancelTitle;
}
- (void)setConfirmTitle:(NSString *)confirmTitle{
    _confirmTitle = confirmTitle;
    self.footerConfimCancelView.confirmTitle = _confirmTitle;
}

- (void)setScrollViewNormalImage:(UIImage *)scrollViewNormalImage{
    _scrollViewNormalImage = scrollViewNormalImage;
    self.headerSelectTabView.normalImage = _scrollViewNormalImage;
}

- (void)setScrollViewSelectImage:(UIImage *)scrollViewSelectImage{
    _scrollViewSelectImage = scrollViewSelectImage;
    self.headerSelectTabView.selectImage = _scrollViewSelectImage;
}

- (UIView *)backView
{
    if (!_backView) {
        _backView = [UIView new];
        _backView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.7];
    }
    return _backView;
}

- (UIView *)contentView
{
    if (!_contentView) {
        _contentView = [UIView new];
        _contentView.backgroundColor = [[DLThemeAdapter shareInstance] dynamicColor:[UIColor whiteColor] darkColor:[UIColor colorWithHex:0x2D2D2E]];
    }
    return _contentView;
}

@end
